#!coding=utf-8

from standard_function import *

def del_appdown_task(driver):
    #清空下载任务
    driver.find_element_by_name("管理").click()
    driver.find_element_by_name("下载管理").click()
    try:
        driver.find_element_by_id("com.qihoo.appstore:id/group_header_edit").click()
        ss = driver.find_elements_by_id("com.qihoo.appstore:id/download_check")
        ss[0].click()
        driver.find_element_by_id("com.qihoo.appstore:id/group_header_del").click()
        driver.find_element_by_id("com.qihoo.appstore:id/common_dialog_positive_btn").click()
    except:
        pass
    driver.back()
    driver.find_element_by_name("首页").click()

def clickappdown(driver):
    try:
        hot_btn = WaitforElementByName(driver,u"最热",30)
    except NoSuchElementException:
        raise NoSuchElementException
    else:
        hot_btn.click()

#    time.sleep(2)
#    swipeUp(driver,0.9,random.randint(0,8)*0.1)
#    time.sleep(2)

    try:
        btns=WaitforElementsById(driver,"com.qihoo.appstore:id/common_list_body_top",30)
            #点击单个应用，进入详情页面点
    except NoSuchElementException:
        raise NoSuchElementException
    else:
        print len(btns)
        for btn in btns:
            try:
                size=WaitforElementById(btn,"com.qihoo.appstore:id/common_list_tv_desc_3",30).get_attribute("text")
            except NoSuchElementException:
                continue
            print size
            if 'G' not in size:
                if 'K' in size or float(size[0:-1])<20:
                    continue
            try:
                down_btn=WaitforElementById(btn,'com.qihoo.appstore:id/common_list_download',30)
            except NoSuchElementException:
                continue
            else:
                if down_btn.get_attribute("name")!=u'下载':
                    continue
                else: 
                    down_btn.click()
                waittime=60
                while waittime:
                    text=''
                    try:
                        text = WaitforElementById(btn,"com.qihoo.appstore:id/download_right_speed_text",2).get_attribute("name")
                    except NoSuchElementException:
                        try:
                            install_btn=WaitforElementById(driver,'com.android.packageinstaller:id/cancel_button',2)
                        except NoSuchElementException:
                            pass
                        else:
                            install_btn.click()
                    else:
                        print text
                        if '0.00%' not in text:
                            down_btn.click()
                            break
                        else:
                            time.sleep(0.5)
                            if waittime==0:
                                down_btn.click()
                                break
                            waittime=waittime-1
                print "----------------------------------------------"



def game(driver):
    driver.find_element_by_name("游戏").click()
    driver.find_element_by_name("分类").click()    # 点击元素
    gamecatetory= [u'卡牌',u'休闲益智',u'角色扮演',u'捕鱼',u'策略',u'射击',u'MOBA',u'棋牌',u'解谜',u'经营',u'消除',u'跑酷',u'挂机',u'养成',u'赛车',u'塔防',u'动作',u'音乐',u'文字冒险',u'模拟',u'网游',]
    time.sleep(2)
    swipeUp(driver,0.9,random.randint(0,8)*0.1)
    time.sleep(2)
    for k in gamecatetory:
        print "Current page is:",k
        try:
            btn = WaitforElementByName(driver,k,1)
        except NoSuchElementException:
            continue
        else:
            btn.click()
            try:
                clickappdown(driver)
            except NoSuchElementException:
                continue
            driver.back()

def soft(driver):
    driver.find_element_by_name("软件").click()
    driver.find_element_by_name("分类").click()    # 点击元素
    time.sleep(2)
    gamecatetory= [u'生活服务',u'购物',u'视频',u'理财',u'社交',u'教育学习',u'旅游酒店',u'运动健康',u'音乐',u'常用工具',u'系统',u'摄影',u'出行',u'阅读',u'育儿母婴',u'办公商务',u'手机美化',u'通讯']
    swipeUp(driver,0.9,random.randint(0,8)*0.1)
    time.sleep(2)
    for k in gamecatetory:
        print "Current page is:",k
        try:
            btn = WaitforElementByName(driver,k,1)
        except NoSuchElementException:
            continue
        else:
            btn.click()
            try:
                WaitforElementByName(driver,u'全部',30).click()
            except NoSuchElementException:
                driver.back()
                continue
            try:
                clickappdown(driver)
            except NoSuchElementException:
                continue
            driver.back()
def run():
    desired_caps = {}
    #desired_caps['automationName'] = 'uiautomator2'
    desired_caps['platformName'] = "Android"               # 声明是ios还是Android系统
    desired_caps['platformVersion'] = ANDROID_VERSION             # Android内核版本号，也可以在模拟器设置中查看
    desired_caps['deviceName'] = DEVICE         # 连接的设备名称，可通过adb devices查看
    desired_caps['appPackage'] = 'com.qihoo.appstore'    # apk的包名
    desired_caps['appActivity'] = '.home.MainActivity'          # apk的launcherActivity
    desired_caps['noReset']='true'                         #不需要再次安装apk
    driver= webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)          # 建立 session
    driver.implicitly_wait(30)
    del_appdown_task(driver)
    soft(driver)
    game(driver)
    driver.quit()

#run()